package com.sinux.generality.basesupport.utils.tool;
import com.sinux.generality.basesupport.utils.exception.BaseReturnEnum;
import com.sinux.generality.basesupport.utils.exception.ReturnEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;

/**
 * @describe  Controller层常用返回 Map<String,Object> 对象工具类
 * @className ServiceUtil
 * @author panxw
 * @date 2020/4/26 14:09
 * @remark
 */
public class ServiceUtil {

    /**
     * code key
     */
    public static final String CODE_KEY = "code";

    /**
     * data key
     */
    public static final String DATA_KEY = "data";

    /**
     * msg key
     */
    public static final String MSG_KEY = "msg";

    /**
     * 日志
     */
    public static final Logger LOGGER = LoggerFactory.getLogger(ServiceUtil.class);

    /**
     * returnSuccess 返回成功
     * @return Map<String,Object>
     */
    public Map<String, Object> returnSuccess() {
        return returnSuccess(null);
    }

    /**
     * returnSuccess 返回成功后的值
     * @param value data值
     * @return Map<String,Object>
     */
    public Map<String, Object> returnSuccess(Object value) {
        Map<String, Object> result = new HashMap<>(16);
        result.put(CODE_KEY, BaseReturnEnum.OPERATE_SUCCESS.getCode());
        result.put(MSG_KEY, BaseReturnEnum.OPERATE_SUCCESS.getMsg());
        if (value != null) {
            result.put(DATA_KEY, value);
        }
        return result;
    }

    /**
     * returnError 返回指定错误信息
     * @param returnEnum 错误枚举
     * @return Map<String,Object>
     */
    public Map<String, Object> returnError(ReturnEnum returnEnum) {
        return returnError(returnEnum.getCode(), returnEnum.getMsg());
    }

    /**
     * returnError 返回指定错误信息
     * @param returnEnum 错误枚举
     * @param value data值
     * @return Map<String,Object>
     */
    public Map<String, Object> returnError(ReturnEnum returnEnum, Object value) {
        Map<String, Object> result = returnError(returnEnum);
        if (value != null) {
            result.put(DATA_KEY, value);
        }
        return result;
    }

    /**
     * returnError 返回指定错误信息
     * @param code 错误code
     * @param msg  错误提示
     * @return Map<String,Object>
     */
    public Map<String, Object> returnError(Integer code, String msg) {
        Map<String, Object> result = new HashMap<>(16);
        result.put(CODE_KEY, code);
        result.put(MSG_KEY, msg);
        return result;
    }

    /**
     * returnError 返回指定错误信息
     * @param code 错误code
     * @param msg  错误提示
     * @param value data值
     * @return Map<String,Object>
     */
    public Map<String, Object> returnError(Integer code, String msg, Object value) {
        Map<String, Object> result = returnError(code, msg);
        if (value != null) {
            result.put(DATA_KEY, value);
        }
        return result;
    }

}
